from sqlalchemy.orm import Session
from . import models, schemas

def get_news_reply(db: Session, news_reply_id: int):
    return db.query(models.NewsReply).filter(models.NewsReply.Id == news_reply_id).first()

def get_news_replies(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.NewsReply).offset(skip).limit(limit).all()

def create_news_reply(db: Session, news_reply: schemas.NewsReplyCreate):
    db_news_reply = models.NewsReply(**news_reply.dict())
    db.add(db_news_reply)
    db.commit()
    db.refresh(db_news_reply)
    return db_news_reply

def update_news_reply(db: Session, news_reply_id: int, news_reply: schemas.NewsReplyCreate):
    db_news_reply = db.query(models.NewsReply).filter(models.NewsReply.Id == news_reply_id).first()
    if db_news_reply:
        for key, value in news_reply.dict().items():
            setattr(db_news_reply, key, value)
        db.commit()
        db.refresh(db_news_reply)
    return db_news_reply

def delete_news_reply(db: Session, news_reply_id: int):
    db_news_reply = db.query(models.NewsReply).filter(models.NewsReply.Id == news_reply_id).first()
    if db_news_reply:
        db.delete(db_news_reply)
        db.commit()
    return db_news_reply